//
//  KZVideoWindowPlayerView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/9/4.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZVideoWindowPlayerView.h"
#import "ZFPlayer.h"
#import "ZFPlayerControlView.h"
#import "ZFAVPlayerManager.h"

@interface KZVideoWindowPlayerView()

@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@property (nonatomic, strong) NSArray <NSURL *> *assetURLs;

@property (nonatomic, strong) UIView *container;

@end

@implementation KZVideoWindowPlayerView

- (void)dealloc {
    NSLog(@"视频播放器销毁");
}

- (instancetype)init {
    
    if (self = [super init]) {
        
    }
    return self;
}

- (void)setupUI {
    
    _container = [UIView new];
    [self addSubview:_container];
    [_container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:_container];
    self.player.controlView = self.controlView;
    /// 设置退到后台继续播放
    self.player.pauseWhenAppResignActive = NO;
    @weakify(self)
    self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        @strongify(self)
        [[self getCurrentViewController] setNeedsStatusBarAppearanceUpdate];
        [[UIApplication sharedApplication] setStatusBarHidden:isFullScreen];
    };

    // 播放完毕退出全屏
    self.player.playerDidToEnd = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset) {
        @strongify(self)
        if (self.player.isFullScreen) {
            [self.player enterFullScreen:false animated:YES];
        }
    };
    self.player.assetURL = [NSURL URLWithString:_url];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.layer.cornerRadius = 4;
    closeBtn.layer.masksToBounds = true;
    closeBtn.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.6];
    [closeBtn setImage:[UIImage imageNamed:@"kz-关闭"] forState:UIControlStateNormal];
    [closeBtn setImageEdgeInsets:UIEdgeInsetsMake(2, 2, 2, 2)];
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(@0);
        make.width.height.equalTo(@25);
    }];
}

- (void)close {
    
    [self hide];
}
- (void)show {
 
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIView *maskView = [UIView new];
    maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.0];
    [window addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
    
    [maskView addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(maskView).multipliedBy(0.8);
        make.height.equalTo(maskView.mas_width).multipliedBy(0.4);
        make.center.equalTo(maskView);
        // 高度根据子视图
    }];
    
    [self layoutIfNeeded];
    [self setupUI];
    
    self.transform = CGAffineTransformMakeTranslation(0, -600);
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformIdentity;
        maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
        
        [self.player playTheIndex:0];
    }];
    
}
- (void)hide {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, 600);
        self.superview.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        if (finished) {
            [self.player stop];
            self.player.viewControllerDisappear = YES;
            [self.superview removeFromSuperview];
        }
    }];
    
}



#pragma mark - getter
- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
    }
    return _controlView;
}

@end
